// Fill out your copyright notice in the Description page of Project Settings.


#include "Group/WidgetGroupLibrary.h"
#include "Group/WidgetGroupManager.h"
#include "WGIUserWidget.h"


UWidgetGroupManager* WidgetGroupLibrary::GroupManager = nullptr;


void WidgetGroupLibrary::EnsureManagerValid(APlayerController* Owner)
{
	if (GroupManager == nullptr)
	{
		GroupManager = new UWidgetGroupManager();
		UE_LOG(WGI_WidgetGroupManagerLog, Log, TEXT("CreateManager"));
	}
}


void WidgetGroupLibrary::ClearWidgetGroupManager()
{
	if (GroupManager)
	{
		GroupManager->ClearWidget();
	}
}

void WidgetGroupLibrary::AddWidgetToGroup(UWGI_UserWidget* inWidget, bool bRemoveLast)
{
	EnsureManagerValid(inWidget->GetOwningPlayer());
	if (bRemoveLast)
	{
		GroupManager->RemvoeLastWidget(false);
	}

	GroupManager->AddWidget(inWidget);
}

void WidgetGroupLibrary::RemoveWidgetFromGroup(UWGI_UserWidget* inWidget, bool bRemoveAll)
{
	EnsureManagerValid(inWidget->GetOwningPlayer());
	if (bRemoveAll)
	{
		ClearWidgetGroupManager();
	}
	else
	{
		GroupManager->RemoveWidget(inWidget);
	}
}

#if WITH_EDITOR

void WidgetGroupLibrary::GameIsEnd(UWGI_UserWidget* inWidget)
{
	if (GroupManager)
	{
		GroupManager->GameIsEnd(inWidget);
	}
}

#endif